package com.xywy.wjk.widget;

import java.util.Timer;
import java.util.TimerTask;

import android.R.interpolator;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xywy.R;
import com.xywy.common.widget.ProgressWheel;

/**
 * 用于通用的提示对话框 只有一个确定按钮
 * 
 * @author wangwenlong
 * 
 */
public class TipDialog {

	private static final String TAG = "TipDialog";
	public static final int VIEW_LOADING = 1;
	public static final int VIEW_DIALOG = 2;
	public static final int VIEW_TIP = 3;
	private int cur_view_layout;
	private Handler mhHandler;
	boolean running;

	private Context context;
	private Dialog dialog;

	private ImageView iv;
	/* 底部两个按钮 */
	private Button btn_cancle;
	private Button btn_confirm;
	/* 底部一个按钮 */
	private Button btn_okay;
	private TextView title; // 标题
	private TextView content; // 内容

	private TimerListenter timerListenter; // 13s方法回调；
	private CancleListener cancleListener;// 取消按钮回调
	private ConfirmListener confirListener;// 确认按钮回调
	private OkayListener okaylistener; // 知道了
	DismissListener dismissListener;
	public static final int DIALOG_TIMER_CANCLE = 0x1000;

	public TipDialog(Context context, int view_layout) {
		super();
		this.context = context;
		init(view_layout, null);
	}

	public TipDialog(Context context, int view_layout, Handler handler) {
		this.context = context;
		this.mhHandler = handler;
		init(view_layout, handler);
	}

	ProgressWheel pw_two;
	int progress = 0;

	private void init(int view_layout, Handler handler) {
		cur_view_layout = view_layout;
		switch (view_layout) {
		case VIEW_LOADING: // 载入动画 ,
			cur_view_layout = VIEW_LOADING;
			dialog = new Dialog(context, R.style.Translucent_NoTitle);
			dialog.setContentView(R.layout.tip_dialog);
			pw_two = (ProgressWheel) dialog.findViewById(R.id.progressBarTwo);
			pw_two.setText("");
			break;
		case VIEW_DIALOG: // 对话窗口
			cur_view_layout = VIEW_DIALOG;
			dialog = new Dialog(context, R.style.Translucent_NoTitle);
			dialog.setContentView(R.layout.tip_dialog_switch);
			title = (TextView) dialog.findViewById(R.id.title);
			content = (TextView) dialog.findViewById(R.id.content);
			btn_cancle = (Button) dialog.findViewById(R.id.btn_cancle);
			btn_confirm = (Button) dialog.findViewById(R.id.btn_confirm);
			btn_cancle.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (cancleListener != null) {
						cancleListener.cancle();
					}
				}
			});
			btn_confirm.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if (confirListener != null) {
						confirListener.confirm();
					}
				}
			});

			break;

		case VIEW_TIP: // 提示：
			cur_view_layout = VIEW_TIP;
			dialog = new Dialog(context, R.style.Translucent_NoTitle);
			dialog.setContentView(R.layout.tip_dialog_tip);
			title = (TextView) dialog.findViewById(R.id.title);
			content = (TextView) dialog.findViewById(R.id.content);
			btn_okay = (Button) dialog.findViewById(R.id.btn_okay);
			btn_okay.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					if (okaylistener != null) {
						okaylistener.okay();
					}
				}
			});
			break;

		}

	}

	Runnable r = new Runnable() {
		public void run() {
			running = true;
			while (progress < 361) {
				pw_two.incrementProgress();
				progress++;
				if (progress == 360) {
					mhHandler.postDelayed(new Runnable() {
						@Override
						public void run() {
							if (dismissListener != null) {
								dismissListener.dismiss();
							}
							dismiss();
						}
					}, 0);
				}
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			running = false;
		}
	};

	/**
	 * 设置当前Dialog展示的标题和内容；
	 * 
	 * @param title
	 * @param content
	 */
	public void setTitleAndContent(String title, String content) {
		if (cur_view_layout == VIEW_DIALOG || cur_view_layout == VIEW_TIP) {
			this.title.setText(title);
			this.content.setText(content);
		} else {
			Log.d(TAG, "当前Dialog为loading");
		}
	}

	/* common function */
	/**
	 * 获取Dialog 引用；
	 * 
	 * @return
	 */
	public Dialog getDialog() {
		return dialog;
	}

	/**
	 * 执行timer13秒 时间到停止计时。
	 * 
	 * @author yangyu
	 * 
	 */
	public interface TimerListenter {
		void timerOver();
	}

	/**
	 * 确定按钮
	 * 
	 * @author yangyu
	 */
	public interface ConfirmListener {
		void confirm();
	}

	/**
	 * 取消按钮
	 * 
	 * @author yangyu
	 */
	public interface CancleListener {
		void cancle();
	}

	/**
	 * 知道了按钮
	 * 
	 * @author yangyu
	 * 
	 */
	public interface OkayListener {
		void okay();
	}

	public interface DismissListener {
		void dismiss();
	}

	public void setOkayCallback(OkayListener okaylistener) {
		this.okaylistener = okaylistener;
	}

	public void setCancleCallback(CancleListener cancleListener) {
		this.cancleListener = cancleListener;
	}

	public void setConfirmCallback(ConfirmListener confirmListener) {
		this.confirListener = confirmListener;
	}

	public void setDismissCallback(DismissListener dismissListener) {
		this.dismissListener = dismissListener;
	}

	Timer timer;

	public void show() {
		if (dialog.isShowing()) {
			return;
		}
		if (cur_view_layout == VIEW_LOADING) {
			progress = 0;
			pw_two.resetCount();
			// Thread s = new Thread(r);
			// s.start();
			timer = new Timer();
			timer.schedule(new TimerTask() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					pw_two.incrementProgress();
					progress++;
					if (progress == 360) {
						mhHandler.postDelayed(new Runnable() {
							@Override
							public void run() {
								if (dismissListener != null) {
									dismissListener.dismiss();
									Message msg = Message.obtain();
									msg.what = DIALOG_TIMER_CANCLE;
									mhHandler.sendMessage(msg);
								}
								dismiss();
							}
						}, 0);
					}
				}
			}, 0, 41);
		}
			this.dialog.show();
	}

	public void dismiss() {
		if (dialog.isShowing()) {
			this.dialog.dismiss();
			if (timer != null)
				timer.cancel();
		}
		progress = 0;
	}

}
